/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.clarice.homechef.service;

import com.clarice.homechef.constants.Constants;
import com.clarice.homechef.dao.BillDao;
import com.clarice.homechef.domain.Bill;
import com.clarice.homechef.domain.Chef;
import com.clarice.homechef.domain.Meal;
import com.clarice.homechef.domain.Orders;
import com.clarice.homechef.domain.User;
import com.clarice.homechef.mail.Email;
import com.clarice.homechef.mail.SMS;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Akshay Velhal
 */
@Service
public class BillServiceImpl implements BillService {

    @Autowired
    UserService userService;
    @Autowired
    BillDao billDao;
    @Autowired
    MealService mealService;
    @Autowired
    ChefService chefService;

    public void createNewBill(final String userName, List<Orders> orders) throws Exception {
        final User user = userService.getUser(userName);
        final Map<String, String> chefEmails = new HashMap<>();
        String reviewUrls = "";
        Bill bill = new Bill();
        bill.setUserId(user.getUserId());
        double amount = 0;
        for (Orders order : orders) {
            Meal meal = mealService.getMeal(order.getMealId());
            order.setChefId(meal.getChefId());
            order.setPrice(meal.getPrice());
            amount += meal.getPrice();
            Chef chef = chefService.getChefById(order.getChefId());
            chefEmails.put(chef.getEmail(), chef.getName());
            reviewUrls +="http://localhost:8080/HomeChef/#/mealreview/" + meal.getMealId() + "\n";
        }
        final String reviewUrl = reviewUrls;
        bill.setAmount(amount);
        bill.setOrders(orders);
        final int billId = billDao.createNewBill(bill);

        new Thread() {
            public void run() {
                //send mail: 1- User gets a order confirmation
                System.out.println("Sending email to user");
                String subject = String.format(Constants.billCreatedConfirmationHeader, billId);
                String body = String.format(Constants.billCreatedConfirmationBody, user.getName(), billId);
                Email email = new Email();
                email.send(userName, subject, body);
                System.out.println("Sent Email to user");
                System.out.println("Sending email to chefs");
                for (Map.Entry<String, String> chefEmail : chefEmails.entrySet()) {
                    //send mail: 2- Chef gets a order confirmation
                    String chefEmailBody = String.format(Constants.chefOrderConfirmationBody, chefEmail.getValue(), billId);
                    email.send(chefEmail.getKey(), subject, chefEmailBody);
                }
                System.out.println("Sent email to chefs");

                System.out.println("Sending User Email for Reviews");
                String reviewSubject = String.format(Constants.reviewRequestSubject, billId);
                String reviewBody = String.format(Constants.reviewRequestBody, user.getName(), billId, reviewUrl);
                email.send(user.getEmail(), reviewSubject, reviewBody);
                System.out.println("Sent User Email for Reviews");
                
                //Uncomment this for sending sms.
                //SMS.sms(user.getMobile(), user.getArea(), "Baner");
            }
        }.start();
    }

    public Bill getCartDetails(String userName, List<Orders> orders) throws Exception {
        User user = userService.getUser(userName);
        Bill bill = new Bill();
        bill.setUserId(user.getUserId());
        double amount = 0;
        for (Orders order : orders) {
            Meal meal = mealService.getMeal(order.getMealId());
            Chef chef = chefService.getChefById(meal.getChefId());
            meal.setChefName(chef.getName());
            meal.setChefAddress(chef.getAddress());
            order.setChefId(meal.getChefId());
            order.setPrice(meal.getPrice());
            order.setMeal(meal);
            amount += meal.getPrice();
        }
        bill.setAmount(amount);
        bill.setOrders(orders);
        bill.setBillUserName(user.getName());
        return bill;
    }
}
